package com.calmcar.adas.serverman;

import android.content.Context;

import com.calmcar.adas.apiserver.AdasConf;
import com.calmcar.adas.apiserver.out.CvCameraViewFrame;
import com.calmcar.adas.conf.Constant;
import com.calmcar.adas.dao.DetectDataInfo;
import com.calmcar.adas.ldw.CarLaneServer;
import com.calmcar.adas.service.AdasServicePool;

public class LaneCarServerManager {
    public LdwServerManager getLdwServerManager() {
        return ldwServerManager;
    }

    public CdwServerManager getCdwServerManager() {
        return cdwServerManager;
    }

    LdwServerManager ldwServerManager;
    CdwServerManager cdwServerManager;
    CarLaneServer carLaneServer;
    Context  mContext;


    public LaneCarServerManager (Context mContext, AdasServicePool adasServicePool) {
        this.mContext = mContext;
        carLaneServer=new CarLaneServer(mContext);
        this.adasServicePool = adasServicePool;
        ldwServerManager=new LdwServerManager(mContext);
        cdwServerManager=new CdwServerManager(mContext);
    }

    AdasServicePool adasServicePool;
    //初始化配置信息
    public  void  init(){
        carLaneServer.initConf();
        carLaneServer.initStart();
    }
    //启动ldwServer
    public void startServer(){
        startServiceThread();
    }

    public void stopServer(){
        carLaneServer.stopServer();
        adasServicePool.stopLaneService();
    }

    public void startServiceThread() {
        initServicePool();
        adasServicePool.createHandlerLaneCar();
    }

    DetectDataInfo detectDataInfo;
    public void initServicePool() {
        adasServicePool.setLaneCarDataListener(new AdasServicePool.LaneCarDataListener() {
            @Override
            public void onProcessData(CvCameraViewFrame inputFrame ) {
                DetectDataInfo detectDataInfoTemp =carLaneServer.laneDetectProcessForCameraView(inputFrame);
                detectDataInfo=detectDataInfoTemp;
                if(  detectDataInfo==null){
                    cdwServerManager. carDataCallBack(null,0);
                    ldwServerManager.laneDataCallBack(null);
                }else{
                    cdwServerManager. carDataCallBack(detectDataInfo.getCarData(),detectDataInfo.getCarType());
                    ldwServerManager. laneDataCallBack(detectDataInfo.getLaneData());
                }
            }
        });
    }


    public void sendFrameData(CvCameraViewFrame inputStream, double rate ){
//        if(AdasConf.LANE_DETECT_OPEN  ){
//            if(rate> Constant.CAR_SPEED_LANE_THRESHOLD) {
                adasServicePool.sendLaneCarData(inputStream);
//            }
//        }
    }




    public int putHsml(String hsmlHead,String hsml){

        return carLaneServer.putHsml(hsmlHead,hsml);
    }

    /**
     * 设置视野焦点
     * @param mv_x
     * @param mv_y
     */
    public void setVPPara (int mv_x,int mv_y,int type ) {

        carLaneServer.setVPParaNew( mv_x,mv_y,type);
    }

    public void setDetectState  (int laneOpen,int carOpen ) {

        carLaneServer.setDetectState(laneOpen,carOpen);
    }

}
